# -*- coding: utf_8 -*-
# @Time : 2021/8/31 0031 16:11
# @Author : lyw
# @Remark : 待修库db
import logging

from mxsoftpy import DB
from mxsoftpy.exception import DBError, DataError,ParamsError

from model.common.VideoManage.VideoQuality.video import VideoRepairCreateModel, VideoRepairDeleteModel



class RepairDB(DB):

    def update(self, video_code: str, create_data: VideoRepairCreateModel) -> str:
        """
        更新待修数据
        :param video_code: 要新增的视频code
        :param create_data: 操作人
        """
        try:
            self.tree.open('VideoQuality', 'Video\\%s' % video_code, file='IOT')
        except DBError as e:
            logging.error('新增到待修库时：打开视频code：%s失败，错误信息：%s' % (video_code, str(e)))
            raise DataError('新增到待修库失败')

        repair_state = self.tree.fetchone_value('repair_state')
        if repair_state == 1:
            raise ParamsError(f'视频{video_code}已存在至待修库')
        self.tree.insert_items(create_data.dict())  # 将待修标志改为 1
        return video_code

    def delete(self, video_code: str, delete_data: VideoRepairDeleteModel) -> str:
        """
        更新待修数据
        :param video_code: 要新增的视频code
        :param delete_data: 操作人
        """
        try:
            self.tree.open('VideoQuality', 'Video\\%s' % video_code, file='IOT')
        except DBError as e:
            logging.error('删除待修库时：打开视频code：%s失败，错误信息：%s' % (video_code, str(e)))
            raise DataError('删除待修库失败')
        repair_state = self.tree.fetchone_value('repair_state')
        if repair_state == 0:
            raise DataError(f'视频{video_code}待修已删除')
        self.tree.insert_items(delete_data.dict())  # 将待修标志改为 0
        return video_code

